; load neccesary files
(load "C:\\users\\lonkz\\.clisprc.lisp")

; map is a list of lists of 4: (x, y, length, height) - in units
(defparameter unit 10)
(defparameter map '((50 20 20 20 sdl:*white*) ; farven bruges ikke lige nu, se draw-buildings*
					(10 30 20 10 sdl:*white*)))

; the location of the car
(defparameter car-loc '(100 100))
; the rotation of the car
(defparameter car-rot 10)
; car speed
(defparameter speed 5)
; width of the car
(defparameter car-w 40)
; height of the car
(defparameter car-h 20)

; uses aux func to draw buildings
(defun draw-buildings ()
	(draw-buildings* map))

; draw all buildings recursively
(defun draw-buildings* (l)
	(if (null l)
		T
		(and (draw-box* (* unit (caar l)) 
						(* unit (cadar l))
						(* unit (caddar l))
						(* unit (car (cdddar l)))) ; fjern rparen
;						(car (cdr (cdddar l))))
			 (draw-buildings* (cdr l)))))
		
; draws a box with given dims and a random color
(defun draw-box* (x y w h)
	(sdl:draw-box (sdl:rectangle-from-midpoint-* x y w h)
		:color sdl:*white*))

; 1=top left, 2=top right, 3=bot left, 4=bot right
(defun rotate (i)
  (cond
    ((= i 1)
	(let ((x (* -1 (/ car-w 2)))
		   (y (* -1 (/ car-h 2))))
		  (rotate-point x y)))
    ((= i 1)
      (let ((x (* 1 (/ car-w 2)))
	        (y (* -1 (/ car-h 2))))
	       (rotate-point x y)))
    ((= i 1)
      (let ((x (* 1 (/ car-w 2)))
	        (y (* 1 (/ car-h 2))))
	       (rotate-point x y)))
    ((= i 1)
      (let ((x (* -1 (/ car-w 2)))
	        (y (* 1 (/ car-h 2))))
	       (rotate-point x y)))))

; calculate the point after (x,y) has been rotated 5 degrees
(defun rotate-point (x y)
  (cons (- (* x (cos (+ car-rot 5))) (* y (sin (+ car-rot 5))))
	(+ (* y (cos (+ car-rot 5))) (* x (sin (+ car-rot 5))))))

; is the car pointed at the first quadrant?
(defun first-quad? ()
	(and (<= car-rot 90)
		 (> car-rot 0)))
		 
; is the car pointed at the second quadrant?
(defun second-quad? ()
	(and (<= car-rot 180)
		 (> car-rot 90)))
		 
; is the car pointed at the third quadrant?
(defun third-quad? ()
	(and (<= car-rot 270)
		 (> car-rot 180)))
		 
; is the car pointed at the fourth quadrant?
(defun fourth-quad? ()
	(and (<= car-rot 360)
		 (> car-rot 270)))

; move car forwards  
(defun car-forwards ()
	(if (= 0 (mod car-rot 90))
		(setf car-rot (+ 1 car-rot)))
	(cond 
		((first-quad?)
;			(if (and (< (car car-loc) 780)
;					 (> (cadr car-loc) 20))
				(setf car-loc `(,(+ (car car-loc) (/ speed (/ 90 (- 90 car-rot))))
								,(+ (cadr car-loc) (/ speed (/ 90 car-rot))))))
;				T))
		((second-quad?)
;			(if (and (> (car car-loc) 20)
;					 (> (cadr car-loc) 20))
				(setf car-loc `(,(+ (car car-loc) (/ speed (/ 90 (- 180 car-rot))))
								,(+ (cadr car-loc) (/ speed (/ 90 (- car-rot 90)))))))
;				T))
		((third-quad?)
;			(if (and (> (car car-loc) 20)
;					 (< (cadr car-loc) 580))
				(setf car-loc `(,(+ (car car-loc) (/ speed (/ 90 (- car-rot 270))))
								,(+ (cadr car-loc) (/ speed (/ 90 (- car-rot 180)))))))
;				T))
		((fourth-quad?)
;			(if (and (< (car car-loc) 780)
;					 (< (cadr car-loc) 580))
				(setf car-loc `(,(+ (car car-loc) (/ speed (/ 90 (- 360 car-rot))))
								,(+ (cadr car-loc) (/ speed (/ 90 (- 270 car-rot)))))))))
;				T))))

; move car backwards
(defun car-backwards ()
	(if (> (car car-loc) 20)
		(setf car-loc `(,(- (car car-loc) 5) ,(cadr car-loc)))
		T))

; increase the car-rot counter - rotate car clockwise
(defun car-rot-right ()
	(setf car-rot (mod (+ car-rot 5) 360)))

; increase the car-rot counter - rotate car counter-clockwise
(defun car-rot-left ()
	(setf car-rot (if (< car-rot 6)
					  360
					  (- car-rot 5))))

; draws the shape of the car
(defun draw-car ()
	(sdl:draw-box 
		(draw-rectangle `#(,(rotate 1) ,(rotate 2) ,(rotate 3) ,(rotate 4)))
		:color sdl:*white*))

; main game loop
(defun mouse-rect-2d ()
	(sdl:with-init ()
		; setup app
		(sdl:window 800 600 :title-caption "Car game!")
;							:no-frame t)
		(setf (sdl:frame-rate) 60)
		(sdl:enable-key-repeat 1 1)
		;(sdl:key-repeat-enabled-p)
		; draw stuff
		(draw-buildings)
		(draw-car)
		; handle events
		(sdl:with-events ()
			;(:quit-event () (quit))
			; handle keyboard input
			(:key-down-event (:key key)
				(when (sdl:key= key :sdl-key-escape)
					(quit))
				(when (sdl:key= key :sdl-key-w)
					(car-forwards))
				(when (sdl:key= key :sdl-key-s)
					(car-backwards))
				(when (sdl:key= key :sdl-key-a)
					(car-rot-left))
				(when (sdl:key= key :sdl-key-d)
					(car-rot-right)))
			; update loop
			(:idle ()
				(when (sdl:mouse-left-p)
					(print "Mouse pressed"))
				(sdl:clear-display sdl:*black*)
				(draw-buildings)
				(draw-car)
				(sdl:update-display)))))

; starts the game by calling the main game loop
(mouse-rect-2d)